package com.ziyun.erp.web.api.goods;

import com.utility.JsonUtils;
import com.ziyun.erp.modules.goods.entity.GoodsBrandEntity;
import com.ziyun.erp.modules.goods.entity.QuoteVO;
import com.ziyun.erp.modules.goods.service.GoodsBrandService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author xueqz
 * @email
 * @date 2018-08-20 09:57:04
 */
@RestController
@RequestMapping("port/goods/goodsbrand")
public class GoodsBrandControllerApi {
    @Autowired
    private GoodsBrandService goodsBrandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map paramBodyMap){
        Map params = (Map) paramBodyMap.get("params");
        PageUtils page = goodsBrandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(@RequestBody Map paramBodyMap){
        //,@PathVariable("brandId") Integer brandId
        Map params = (Map) paramBodyMap.get("params");
        Integer brandId= (Integer) params.get("brandId");
        GoodsBrandEntity goodsBrand = goodsBrandService.selectById(brandId);
        return R.ok().put("goodsBrand", goodsBrand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody Map paramBodyMap){
        //@RequestBody GoodsBrandEntity goodsBrand
        Map params = (Map) paramBodyMap.get("params");
        String goodsBrand = JsonUtils.object2String(params.get("goodsBrand"));
        GoodsBrandEntity goodsBrandEntity = JsonUtils.string2Object(goodsBrand, GoodsBrandEntity.class);
        goodsBrandService.insert(goodsBrandEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody Map paramBodyMap){
//        ValidatorUtils.validateEntity(goodsBrand);
        Map params = (Map) paramBodyMap.get("params");
        String goodsBrand = JsonUtils.object2String(params.get("goodsBrand"));
        GoodsBrandEntity goodsBrandEntity = JsonUtils.string2Object(goodsBrand, GoodsBrandEntity.class);
        goodsBrandService.updateAllColumnById(goodsBrandEntity);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] brandIds){
        goodsBrandService.deleteBatchIds(Arrays.asList(brandIds));
        return R.ok();
    }

    /**
     * 全部品牌
     */
    @RequestMapping("/allList")
    public R allList(@RequestBody Map paramBodyMap){
        Map params = (Map) paramBodyMap.get("params");

//        PageUtils page = goodsBrandService.queryPage(params);
        HashMap<String, Object> map = new HashMap<>();
        map.put("disabled","false");
        map.put("seller_id",params.get("seller_id"));
        List<GoodsBrandEntity> brandEntityList = goodsBrandService.selectByMap(map);
        return R.ok().put("brandEntityList", brandEntityList);
    }

}
